<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>爱家-发布新房源</title>
    <link href="/static/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="/static/css/reset.css" rel="stylesheet">
    <link href="/static/plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css" rel="stylesheet">
    <link href="/static/css/ihome/main.css" rel="stylesheet">
    <link href="/static/css/ihome/newhouse.css" rel="stylesheet">
</head>

<body>
    <div class="container">
        <div class="top-bar">
            <div class="nav-bar">
                <h3 class="page-title">发布新房源</h3>
                <a class="nav-btn fl" href="/myhouse.html"><span><i class="fa fa-angle-left fa-2x"></i></span></a>
            </div>
        </div>
        <div class="houses-con">
            <ul class="houses-list">
                <form id="form-house-info">
                    <li>
                        <div class="house-title">
                            <h3>基本信息</h3>
                        </div>
                        <div class="house-content">
                            <div class="form-group">
                                <label for="house-title">房屋标题</label>
                                <input type="text" class="form-control" name="title" id="house-title" required>
                            </div>
                            <div class="form-group">
                                <label for="house-price">每晚价格</label>
                                <input type="number" class="form-control" name="price" id="house-price" required>
                            </div>
                            <div class="form-group">
                                <label for="area-id">所在城区</label>
                                <select class="form-control" id="area-id" name="area_id">
                                    <script type="text/html" id="areas-list-tmpl">
                                        {{each areas}}
                                        <option value="{{$value.id}}">{{ $value.name }}</option>
                                        {{/each}}
                                    </script>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="house-address">详细地址</label>
                                <textarea class="form-control" name="address" id="house-address" required></textarea>
                            </div>
                        </div>
                    </li>
                    <li>
                        <div class="house-title">
                            <h3>详细信息</h3>
                        </div>
                        <div class="house-content">
                            <div class="form-group">
                                <label for="house-room-count">出租房间数目</label>
                                <input type="number" class="form-control" name="room_count" id="house-room-count"
                                    required>
                            </div>
                            <div class="form-group">
                                <label for="house-acreage">房屋面积</label>
                                <input type="number" class="form-control" name="acreage" id="house-acreage" required>
                            </div>
                            <div class="form-group">
                                <label for="house-unit">户型描述</label>
                                <input type="text" class="form-control" name="unit" id="house-unit"
                                    placeholder="如：三室两厅两卫" required>
                            </div>
                            <div class="form-group">
                                <label for="house-capacity">宜住人数</label>
                                <input type="number" class="form-control" name="capacity" id="house-capacity" required>
                            </div>
                            <div class="form-group">
                                <label for="house-beds">卧床配置</label>
                                <input type="text" class="form-control" name="beds" id="house-beds"
                                    placeholder="如：双人床:2x1.8x1张 1.5x2x2张" required>
                            </div>
                            <div class="form-group">
                                <label for="house-deposit">押金数额</label>
                                <input type="number" class="form-control" name="deposit" id="house-deposit" required>
                            </div>
                            <div class="form-group">
                                <label for="house-min-days">最少入住天数</label>
                                <input type="number" class="form-control" name="min_days" id="house-min-days" required>
                            </div>
                            <div class="form-group">
                                <label for="house-max-days">最多入住天数</label>
                                <input type="number" class="form-control" name="max_days" id="house-max-days"
                                    placeholder="0表示无限制" required>
                            </div>
                        </div>
                    </li>
                    <li>
                        <div class="house-title">
                            <h3>配套设施</h3>
                        </div>
                        <div class="house-content">
                            <ul class="house-facility-list clearfix">
                                <div id="device-tmpl">
                                    {{each devices }}
                                    <li>
                                        <div class="checkbox">
                                            <label>
                                                <input type="checkbox" name="facility" value="{{$value.id}}">{{ $value.name }}
                                            </label>
                                        </div>
                                    </li>
                                    {{/each}}
                                </div>
                            </ul>
                        </div>
                    </li>
                    <input type="submit" class="btn btn-success btn-commit" value="发布房源信息">
                    <div class="error-msg text-center"><i class="fa fa-exclamation-circle"></i>请将全部信息填写完整后再提交</div>
                </form>
                <form id="form-house-image" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="house_id" id="house-id" value="">
                    <li>
                        <div class="house-title">
                            <h3>添加图片</h3>
                        </div>
                        <div class="house-content">
                            <div class="house-image-cons">
                            </div>
                            <div class="form-group">
                                <label for="house-image">选择图片</label>
                                <input type="file" accept="image/*" name="house_image" id="house-image">
                            </div>
                            <input type="submit" class="btn btn-success" value="上传">
                        </div>
                    </li>
                </form>
            </ul>
        </div>
        <div class="popup_con">
            <div class="popup">
                <p><i class="fa fa-spinner fa-spin fa-3x fa-fw"></i></p>
            </div>
            <div class="mask"></div>
        </div>
        <div class="footer">
            <p><span><i class="fa fa-copyright"></i></span>爱家租房&nbsp;&nbsp;享受家的温馨</p>
        </div>
    </div>

    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/jquery.form.min.js"></script>
    <script src="/static/plugins/bootstrap/js/bootstrap.min.js"></script>
    <script src="/static/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>
    <script src="/static/plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script>
    <script src="/static/js/template.js"></script>
    <script>
        function getCookie(name) {
            var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
            return r ? r[1] : undefined;
        }

        $(document).ready(function () {
            // 向后端获取城区信息
            $.get("/api/v1/areas", function (resp) {
                if (resp.errno == "0") {
                    // 使用js模板
                    $("#area-id").html(template("areas-list-tmpl", { areas: resp.data }));

                } else {
                    alert(resp.errmsg);
                }
            }, "json");

            $.get("/api/v1/devices", function (resp) {
                if (resp.errno == "0") {
                    // 使用js模板
                    var html = template("device-tmpl", { devices: resp.data })
                    $(".house-facility-list").html(html);
                } else {
                    alert(resp.errmsg);
                }
            }, "json");

            $("#form-house-info").submit(function (e) {
                e.preventDefault();

                // 处理表单数据
                var data = {};
                $("#form-house-info").serializeArray().map(function (x) { data[x.name] = x.value });

                // 收集设置id信息
                var facility = [];
                $(":checked[name=facility]").each(function (index, x) { facility[index] = $(x).val() });

                data.devices = facility;

                // 向后端发送请求
                $.ajax({
                    url: "/api/v1/houses/add",
                    type: "post",
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    dataType: "json",
                    headers: {
                        "X-CSRFToken": getCookie("csrf_token")
                    },
                    success: function (resp) {
                        if (resp.errno == "4001") {
                            // 用户未登录
                            location.href = "/login.html";
                        } else if (resp.errno == "0") {
                            // 隐藏基本信息表单
                            $("#form-house-info").hide();
                            // 显示图片表单
                            $("#form-house-image").show();
                            // 设置图片表单中的house_id
                            $("#house-id").val(resp.data.house_id);
                        } else {
                            alert(resp.errmsg);
                        }
                    }
                })
            });

            $("#form-house-image").submit(function (e) {
                e.preventDefault();
                $(this).ajaxSubmit({
                    url: "/api/v1/houses/image",
                    type: "post",
                    dataType: "json",
                    headers: {
                        "X-CSRFToken": getCookie("csrf_token"),
                    },
                    success: function (resp) {
                        if (resp.errno == "4001") {
                            location.href = "/login.html";
                        } else if (resp.errno == "0") {
                            $(".house-image-cons").append('<img src="' + resp.data.image_url + '">');
                        } else {
                            alert(resp.errmsg);
                        }
                    }
                })
            })
        })
    </script>
</body>

</html>